//
//  JDDeliveryInfoViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/8/22.
//

import UIKit
/// 配送信息 页面
class JDDeliveryInfoViewController: JDBaseViewController {

    var deliveryStatusLabel: UILabel?
    var lastStatusTimeLabel: UILabel?
    
    var deliveryCompanyView: UIView?
    var deliveryCompanyName: UILabel?
    var deliveryId: UILabel?
    var copyDeliveryIdButton: UIButton?
    
    var deliveryTableView: UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setConfig()
        self.setupUIElements()
        // Do any additional setup after loading the view.
    }
    
    func setConfig() {
        self.setupLeftBackItem()
        self.title = "运单号详情"
        self.view.backgroundColor = .commonColor()
    }
    
    func setupUIElements() {

        self.deliveryStatusLabel = UILabel()
        self.view.addSubview(self.deliveryStatusLabel!)
        self.deliveryStatusLabel?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view).offset(29)
            make.height.equalTo(40)
        })
        self.deliveryStatusLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 28, weight: .semibold), textColor: color("#333333"), numberOfLines: 1)
        self.deliveryStatusLabel?.text = "运输中"
        
        self.lastStatusTimeLabel = UILabel()
        self.view.addSubview(self.lastStatusTimeLabel!)
        self.lastStatusTimeLabel?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.deliveryStatusLabel!.snp.bottom).offset(6)
            make.height.equalTo(20)
        })
        self.lastStatusTimeLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 14, weight: .semibold), textColor: color("#333333"), numberOfLines: 1)
        self.lastStatusTimeLabel?.text = "08-25 05:22"
        
        self.deliveryCompanyView = UIView()
        self.view.addSubview(self.deliveryCompanyView!)
        self.deliveryCompanyView?.snp.makeConstraints({ make in
            make.left.equalTo(self.view).offset(12)
            make.right.equalTo(self.view).offset(-12)
            make.top.equalTo(self.lastStatusTimeLabel!.snp.bottom).offset(18)
            make.height.equalTo(75)
        })
        self.deliveryCompanyView?.layer.cornerRadius = 8
        self.deliveryCompanyView?.backgroundColor = .white
        
        self.deliveryCompanyName = UILabel()
        self.deliveryCompanyView?.addSubview(self.deliveryCompanyName!)
        self.deliveryCompanyName?.snp.makeConstraints({ make in
            make.left.equalTo(self.deliveryCompanyView!).offset(12)
            make.top.equalTo(self.deliveryCompanyView!).offset(15)
            make.height.equalTo(20)
        })
        self.deliveryCompanyName?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        self.deliveryCompanyName?.text = "递四方货运代理"
        
        self.deliveryId = UILabel()
        self.deliveryCompanyView?.addSubview(self.deliveryId!)
        self.deliveryId?.snp.makeConstraints({ make in
            make.left.equalTo(self.deliveryCompanyView!).offset(12)
            make.height.equalTo(22)
            make.top.equalTo(self.deliveryCompanyName!.snp.bottom).offset(2)
        })
        self.deliveryId?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        self.deliveryId?.text = "UJ538879899YP"

        self.copyDeliveryIdButton = UIButton(type: .custom)
        self.deliveryCompanyView?.addSubview(self.copyDeliveryIdButton!)
        self.copyDeliveryIdButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.deliveryCompanyView!).offset(-12)
            make.centerY.equalTo(self.deliveryId!)
        })
        self.copyDeliveryIdButton?.buttonConfig(normalTitle: "复制", normalTitleColor: color("#FA6161"), titleFont: UIFont.systemFont(ofSize: 14, weight: .regular))
        
        self.deliveryTableView = UITableView.init(frame: .zero, style: .plain)
        self.view.addSubview(self.deliveryTableView!)
        self.deliveryTableView?.snp.makeConstraints({ make in
            make.left.equalTo(self.view).offset(12)
            make.right.equalTo(self.view).offset(-12)
            make.top.equalTo(self.deliveryCompanyView!.snp.bottom).offset(10)
            make.bottom.equalTo(self.view).offset(-10)
        })
        self.deliveryTableView?.backgroundColor = .commonColor()
        self.deliveryTableView?.delegate = self
        self.deliveryTableView?.dataSource = self
        self.deliveryTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
        self.deliveryTableView?.separatorStyle = .none
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension JDDeliveryInfoViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell")
        
        for subV in cell?.contentView.subviews ?? [UIView]() {
            subV.removeFromSuperview()
        }
        
        let bigTitle = UILabel()
        cell?.contentView.addSubview(bigTitle)
        if indexPath.row == 0 {
            bigTitle.snp.makeConstraints { make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.top.equalTo(cell!.contentView)
                make.height.equalTo(25)
            }
        } else {
            bigTitle.snp.makeConstraints { make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.top.equalTo(cell!.contentView)
                make.height.equalTo(0)
            }
        }
        bigTitle.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 18, weight: .regular), textColor: color("#FA6161"), numberOfLines: 1)
        bigTitle.text = "运输中"
        
        
        let timeLabel = UILabel()
        cell?.contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(cell!.contentView).offset(12)
            make.top.equalTo(bigTitle.snp.bottom).offset(1)
            make.height.equalTo(22)
        }
        timeLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color(""), numberOfLines: 1)
        timeLabel.text = "08-24 05:22"
        
        let statusLabel = UILabel()
        cell?.contentView.addSubview(statusLabel)
        statusLabel.snp.makeConstraints { make in
            make.left.equalTo(cell!.contentView).offset(12)
            make.right.equalTo(cell!.contentView).offset(-12)
            make.top.equalTo(timeLabel.snp.bottom).offset(4)
            make.bottom.equalTo(cell!.contentView).offset(-26)
        }
        statusLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        statusLabel.text = "大连 · 快件正发往转运中心"
        return cell!
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    
    
    
    
}
